//
//  HLHomeStatuses.swift
//  weibo-snh
//
//  Created by 黄力 on 15/12/20.
//  Copyright © 2015年 HLTest. All rights reserved.
//

import UIKit

class HLHomeStatuses: NSObject {
    //微博id
    var id: Int64 = 0
    //发送时间
    var created_at: String?
    //微博文本内容
    var text: String?
    //来源
    var source: String?
    //图片
    var pic_urls: [[String: String]]?
    //用户数据模型
    var user: HLHomeUser?
    //转发微博
    var retweeted_status: HLHomeStatuses?
    
    //返回图片地址数组的计算性属性
    var imageURLArr: [NSURL]? {
        get {
            //判断是否有图片
            guard let dicArr = pic_urls else{
                return nil
            }
            //创建接收地址的字典
            var urlArr = [NSURL]()
            //遍历字典
            for dic in dicArr {
                //取出地址字符串
                var urlStr = dic["thumbnail_pic"]
                //将图片替换为高清
                urlStr = urlStr?.stringByReplacingOccurrencesOfString("thumbnail", withString: "large")
                //转换成图片地址
                let url = NSURL(string: urlStr ?? "")
                //保存到可变数组
                if let iUrl = url {
                    urlArr.append(iUrl)
                }
            }
            return urlArr
        }
    }
    
    //字典转模型
    init(dic: [String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dic)
    }
    
    override func setValue(value: AnyObject?, forKey key: String) {
        //当key是status时再进行字典转模型
        if key.compare("retweeted_status") == .OrderedSame {
            //判断是否有转发微博
            if let statusDic = value as? [String: AnyObject] {
                retweeted_status = HLHomeStatuses(dic: statusDic)
            }
            return
        }
        //当key是user时,在进行一次字典转模型
        if key.compare("user") == .OrderedSame {
            //判断是否有数据
            if let userDic = value as? [String: AnyObject] {
                //字典转模型
                user = HLHomeUser(dic: userDic)
            }
            return
        }
        super.setValue(value, forKey: key)
    }
    
    //抛出没有找到的key
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
}
